
DEFINE VARIABLE strLine AS CHARACTER .

DEFINE VARIABLE str-Ma AS CHARACTER .

DEFINE VARIABLE str-n AS CHARACTER .

DEFINE VARIABLE str-num AS CHARACTER .

DEFINE VARIABLE str-user AS CHARACTER .

DEFINE VARIABLE str-user1 AS CHARACTER .

DEFINE VARIABLE cur-Ma AS INTEGER.

DEFINE VARIABLE cur-n AS INTEGER.

DEFINE VARIABLE pre-num AS INTEGER.

DEFINE VARIABLE cur-num AS INTEGER.

DEFINE VARIABLE i AS INTEGER .

DEFINE VARIABLE j AS INTEGER .

DEFINE VARIABLE user-id AS CHARACTER .
DEFINE VARIABLE user-list AS CHARACTER .


user-list = "hudz,fdn,rilison,zhouy,zyf,gaolx,mengm,pengj,tzf,lql,yml,ljb,yfei,yanghju," + 
    "autocheng,cxy,liusw,fcl,zsl,lhy,crh,ljr,zhshx,shsm,ye,dyz,daizq,cht,wuhx,cht,xqsh" .

str-Ma = "Maximum Number of Clients Per Server (-Ma): ".

str-n = "Maximum Number of Users (-n): ".

str-num = "usernum " .

str-user = "Previous message sent on behalf of user ".

str-user1 = "usernum=".
  
INPUT FROM VALUE( "c:\qadguicli\auditrep\mfgxmy.lg" ).

OUTPUT TO VALUE ( "c:\qadguicli\auditrep\mfgxmy_new1.lg" ).

REPEAT ON ERROR UNDO, LEAVE :

    IMPORT UNFORMATTED strLine .
/*
    IF strLine MATCHES ("*Server disconnecting user*") THEN
        strLine = "ignore" .
*/
    IF strLine MATCHES ("* userid *") THEN DO:

        RUN getUserid( INPUT strLine, OUTPUT user-id ) .

/*         IF LOOKUP(user-id, user-list, ",") = 0 THEN  */
/*             strLine = "ignore" .                     */

    END.


    IF strLine MATCHES( "*" + str-Ma + "*") THEN DO:

        RUN getNumber( INPUT strLine, INPUT str-Ma, OUTPUT cur-Ma) .

        IF cur-Ma > 15 THEN DO:

            strLine = REPLACE( strLine, str-Ma + STRING(cur-Ma), str-Ma + "15" ).

        END.

    END.
    ELSE IF cur-Ma > 15 THEN DO:

        IF strLine MATCHES ("*" + str-n + "*") THEN DO:

            RUN getNumber( INPUT strLine, INPUT str-n, OUTPUT cur-n) .

            strLine = REPLACE( strLine, str-n + STRING(cur-n), str-n + "61" ).

        END.

        IF strLine MATCHES ("* user *") THEN DO:

            RUN getNumber( INPUT strLine, INPUT " user ", OUTPUT pre-num) .

            cur-num = pre-num - 4 * (cur-Ma - 15) .

            IF cur-num >= 20 THEN DO:

                strLine = REPLACE( strLine, " user " + STRING(pre-num), " user " + STRING(cur-num) ).

            END.
            ELSE
                strLine = "ignore".
        END.

        IF strLine MATCHES ("*" + str-num + "*") THEN DO:

            RUN getNumber( INPUT strLine, INPUT str-num, OUTPUT pre-num) .

            cur-num = pre-num - 4 * (cur-Ma - 15) .

            IF cur-num >= 20 THEN DO:

                strLine = REPLACE( strLine, str-num + STRING(pre-num), str-num + STRING(cur-num) ).

            END.
            ELSE
                strLine = "ignore".
        END.

        IF strLine MATCHES ("*" + str-user + "*") THEN DO:

            RUN getNumber( INPUT strLine, INPUT str-user, OUTPUT pre-num) .

            cur-num = pre-num - 4 * (cur-Ma - 15) .

            IF cur-num >= 20 THEN DO:

                strLine = REPLACE( strLine, str-user + STRING(pre-num), str-user + STRING(cur-num) ).

            END.
            ELSE
                strLine = "ignore".
        END.

        IF strLine MATCHES ("*" + str-user1 + "*") THEN DO:

            RUN getNumber( INPUT strLine, INPUT str-user1, OUTPUT pre-num) .

            cur-num = pre-num - 4 * (cur-Ma - 15) .

            IF cur-num >= 20 THEN DO:

                strLine = REPLACE( strLine, str-user1 + STRING(pre-num), str-user1 + STRING(cur-num) ).

            END.
            ELSE
                strLine = "ignore".
        END.

    END.

    IF strLine <> "ignore" THEN DO:

        IF strLine = "" THEN
            PUT "" SKIP.
        ELSE
            PUT UNFORMATTED strLine SKIP .
        
    END.

END.

OUTPUT CLOSE.

INPUT CLOSE.

PROCEDURE getNumber :

    DEFINE INPUT PARAMETER ln AS CHARACTER.
    DEFINE INPUT PARAMETER pt AS CHARACTER.
    DEFINE OUTPUT PARAMETER num AS INTEGER.

    DEFINE VARIABLE idx AS INTEGER .

    DEFINE VARIABLE str AS CHARACTER .

    idx = R-INDEX( ln, pt) .

    str = SUBSTRING( ln, idx + LENGTH(pt)) .

    num = 0 .

    DO idx = 1 TO LENGTH(str) :

        IF (ASC( SUBSTRING(str, idx, 1)) >= ASC("0")) AND (ASC( SUBSTRING(str, idx, 1)) <= ASC("9")) THEN
            num = num * 10 + INTEGER(SUBSTRING(str, idx, 1)).
        ELSE
            LEAVE.
    END.

END PROCEDURE. 

PROCEDURE getUserid :

    DEFINE INPUT PARAMETER ln AS CHARACTER.
    DEFINE OUTPUT PARAMETER usrid AS CHARACTER.

    DEFINE VARIABLE pt AS CHARACTER.

    DEFINE VARIABLE idx AS INTEGER .

    DEFINE VARIABLE str AS CHARACTER .

    pt = " userid ".

    idx = R-INDEX( ln, pt) .

    str = SUBSTRING( ln, idx + LENGTH(pt)) .

    usrid = "" .

    DO idx = 1 TO LENGTH(str) :

        IF ( SUBSTRING(str, idx, 1) <> ",") THEN
            usrid = usrid + SUBSTRING(str, idx, 1) .
        ELSE
            LEAVE.

    END.

END PROCEDURE. 
